package org.dromara.demo.controller.redis;

import lombok.RequiredArgsConstructor;
import org.dromara.common.redis.utils.RedisUtils;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;

/**
 * Redis 实现延时队列示例
 *
 * @author Lion Li
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/redis/deque")
public class RedisDequeController {

    /**
     * 发布消息
     *
     * @param key 通道Key
     */
    @GetMapping("/deque")
    public void deque(String key) {
        //创建阻塞队列
        RBlockingDeque<String> blockingDeque = RedisUtils.createBlockingDeque(key);
        //关联阻塞队列
        RDelayedQueue<String> queue = RedisUtils.unionDeque(blockingDeque);
        queue.offer("aaaaaaa", 10, TimeUnit.SECONDS);
        //消费消息
        while (true) {
            try {
                //获取并移除队首元素，如果队列为空，则阻塞等待
                String take = blockingDeque.take();
                //消费消息
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
